package com.linchong.concurrency.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author linchong
 * @version 1.0
 * @Date: 2020-12-08 23:38
 * @Description: CyclicBarrierExample1$
 */
@Slf4j
public class CyclicBarrierExample1 {

	private static CyclicBarrier  barrier = new CyclicBarrier(5);

	public static void main(String[] args) throws InterruptedException {
		ExecutorService exec = Executors.newCachedThreadPool();

		for (int i = 0; i < 10; i++) {
			final int threadNum = i;
			Thread.sleep(1_000);
			exec.execute(()->{
				try {
					race(threadNum);
				} catch (Exception e) {
					log.error("exception",e);
				}
			});
		}
		exec.shutdown();
	}

	private static void race(int threadNum) throws Exception{
		Thread.sleep(1000);
		log.info("{} is ready",threadNum);
		barrier.await();
		log.info("{} continue",threadNum);
	}
}
